from tkinter import *
import requests
import re
import urllib.parse
import json

url="http://192.168.1.1"
url1='http://192.168.1.1/getpage.gch?pid=1002&nextpage=status_lan_host_info_BJ_t.gch'
url2="http://192.168.1.1/QuickMacFilter.gch"

url3="http://192.168.1.1/getpage.gch?pid=1002&nextpage=user_sec_macfilter_conf_t.gch"

headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","Cookie" : "_TESTCOOKIESUPPORT=1"}




r1=r'document.getElementById\("Frm_Logintoken"\).value = "(.*)";'
r2=r'document.getElementById\("Frm_Loginchecktoken"\).value = (\d+);'
r3=r'var session_token = "(\d+)";'

r4=r"Transfer_meaning\('SrcMacAddr\d+','(.*)'\);"

r6=r"\\x\d[a-z]"
def postdata(ck,source):
	jk=re.search(ck,source)
	return jk.groups()[0]


def getCook(url,hh,headers):
	res=requests.post(url, data = hh,headers=headers)
	uu=res.cookies.get_dict()
	del uu['_TESTCOOKIESUPPORT']
	return uu




def dicttostr(dd):
	return 'SID='+dd['SID']


def laihei(url1,data1,headers1):
	resi=requests.post(url1,data=data1,headers=headers1)
	return resi.text	

def jielahei(url1,data1,headers1):
	requests.post(url1,data=data1,headers=headers1)

def msgShow():
	label.config(text="I love Python",bg='lightyellow',fg='blue')





root=Tk()
root.title('ch4_2')
root.geometry("250x250+150+150")


namel1=Label(root,text="z11max ")
namel1.grid(row=0)

namel2=Label(root,text="v18v18 ")
namel2.grid(row=1)
#macE1=Entry(root,show="*")
macE1=Entry(root)

def tihuan(fg):
    oo=[]
    for i in fg:
        oo.append(re.sub(r6,'-',i))
    return oo	


def zonglahei(vb):
	r=requests.get(url)
	hh={"Frm_Logintoken":postdata(r1,r.text),"Frm_Loginchecktoken":postdata(r2,r.text),"_cu_url":0,"Right":2,"Username":'',"UserRandomNum":95670920,"Password":"0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f","action":"login"}
	dicc=getCook(url,hh,headers)#获取SID

	headers1={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","Cookie":dicttostr(dicc)}#不应该是字典的
	data1={"IF_ACTION":"setdata","IF_MAC":vb}
	return laihei(url2,data1,headers1)

###'IF_INDEX':0,'IF_INSTNUM':1，表示拉黑名单中的索引和个数

#造成解除拉黑局面的情况，是由于datajie数据并不是固定的和拉黑页面中的mac个数有关。
#因此提交不对应的datajie会导致不对应的解除拉黑


def fanhuiyonghu():
    r=requests.get(url)
    hh={"Frm_Logintoken":postdata(r1,r.text),"Frm_Loginchecktoken":postdata(r2,r.text),"_cu_url":0,"Right":2,"Username":'',"UserRandomNum":95670920,"Password":"0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f","action":"login"}
    dicc=getCook(url,hh,headers)#获取SID
    ret=requests.get(url3,cookies=dicc)#使用字典携带cookie来get请求,为了从ret取出token放进datajie
    return tihuan(re.findall(r4,ret.text))


def zongjiechu(vb):


    r=requests.get(url)
    hh={"Frm_Logintoken":postdata(r1,r.text),"Frm_Loginchecktoken":postdata(r2,r.text),"_cu_url":0,"Right":2,"Username":'',"UserRandomNum":95670920,"Password":"0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f","action":"login"}
    dicc=getCook(url,hh,headers)#
    ret=requests.get(url1,cookies=dicc)#使用字典携带cookie来get请求,为了从ret取出token放进datajie
    headers1={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","Cookie":dicttostr(dicc)}#不应该是字典的 
   # datajie={'IF_ACTION':'delete','IF_ERRORSTR':'SUCC', 'IF_ERRORPARAM':'SUCC','IF_ERRORTYPE':-1,'IF_INDEX':0,'IF_INSTNUM':1, 'Enable':'','BlackList':'','Type':'','Protocol':'','SrcMacAddr':'', 'DstMacAddr':'','Port':'','Enable0':1,'BlackList0':0,'Type0':'Bridge+Route', 'Protocol0':'ALL','SrcMacAddr0':macE1.get(), 'DstMacAddr0':'','Port0':0,'IsWanSrvCntl':0, 'IpFilterTarget':0,'UrlFilterTarget':0,'UrlFilterEnable':0,'SrvCntlTarget':1, 'DefaultPolicy':0,'MacFilterTarget':'Discard','MacFilterEnable':1,'_SESSION_TOKEN':re.search(r3,ret.text).groups()[0]}
    datajie={'IF_ACTION':'delete','IF_ERRORSTR':'SUCC','IF_ERRORPARAM':'SUCC','IF_ERRORTYPE':-1,
    'IF_INDEX':0,'IF_INSTNUM':1,'Enable':'','BlackList':'','Type':'','Protocol':'',
    'SrcMacAddr':'','DstMacAddr':'','Port':'','Enable0':1,'BlackList0':0,'Type0':'Bridge+Route',
    'Protocol0':'ALL','SrcMacAddr0':vb,'DstMacAddr0':'','Port0':0,'IsWanSrvCntl':0,
    'IpFilterTarget':0,'UrlFilterTarget':0,'UrlFilterEnable':0,'SrvCntlTarget':1,'DefaultPolicy':0,
    'MacFilterTarget':'Discard','MacFilterEnable':1,'_SESSION_TOKEN':re.search(r3,ret.text).groups()[0]}
    #print(re.search(r3,ret.text).groups()[0])
    #print(urllib.parse.urlencode(datajie))
    #print(dicc)
    #print(type(macE1.get()))
    print(vb)
    jielahei(url3,datajie,headers1)







#macE2=Entry(root,show="*")
macE2=Entry(root)


macE1.insert(0,"90:c7:d8:98:5b:d8")
macE2.insert(0,"dc:f0:90:b0:3f:88")

macE1.grid(row=0,column=1)
macE2.grid(row=1,column=1)



def lahei1():
	#print("拉黑1")
	#blahei1["text"]="解除"
	#print(blahei1["text"])
	if blahei1["text"]=="解除":
		#jielaihei(url1,datajie,headers1)
		zongjiechu(macE1.get())
		print("解除mac1")
		blahei1["text"]="拉黑"
		
	elif blahei1["text"]=="拉黑":
		f=json.loads(zonglahei(macE1.get()))
		print(f['result'])
		if f['result']=='SUCC':
		    blahei1["text"]="解除"
    # else:
    #     print("error")



def lahei2():
	#print("拉黑2")
	if blahei2["text"]=="拉黑":
		#blahei2["text"]="解除"
		f=json.loads(zonglahei(macE2.get()))
		print(f['result'])
		if f['result']=='SUCC':
		    blahei2["text"]="解除"		
	elif blahei2["text"]=="解除":
		#blahei2["text"]="拉黑"	
	#blahei2["text"]="解除"
	
		zongjiechu(macE2.get())
		print("解除mac2")
		blahei2["text"]="拉黑"


blahei1=Button(root,text="解除",command=lahei1)
blahei2=Button(root,text="解除",command=lahei2)


blahei1.grid(row=0,column=2)
blahei2.grid(row=1,column=2)


#fruits=["Banana","Apple","Orange"]


lb=Listbox(root)

def llis():
    fruits=fanhuiyonghu()
    if fruits!=[]:
        print(fruits)
        for fruit in fruits:
            print(fruit)
            lb.insert(END,fruit)
    else:
        lb.insert(END,'empty')        
    #lb.grid(pady=10)
    lb.grid(row=2,column=1)
blahei3=Button(root,text="刷新",command=llis)
blahei3.grid(row=2,column=1)
root.mainloop()